package com.hk.core.autoconfigure.data;

import com.hk.commons.multischema.DataSourceType;
import com.hk.commons.util.StringUtils;
import com.hk.core.authentication.api.UserPrincipal;

import java.util.Collections;
import java.util.Map;
import java.util.function.Function;

/**
 * @author Kevin
 * @date 2024-04-21 12:48
 */
public final class PrincipalAccountResolverProvider implements TenantIdResolverProvider {

    private Map<String, String> accountTenantIdMap;

    public PrincipalAccountResolverProvider setAccountTenantIdMap(Map<String, String> accountTenantIdMap) {
        this.accountTenantIdMap = Collections.unmodifiableMap(accountTenantIdMap);
        return this;
    }

    @Override
    public boolean test(String value) {
        return StringUtils.equals(DataSourceType.PRINCIPAL_ACCOUNT, value);
    }

    @Override
    public Function<UserPrincipal, String> getProvider() {
        return principal -> accountTenantIdMap.get(principal.getAccount());
    }
}
